layui.define(['table', 'form','my_send'], function(exports){
  var $ = layui.$
  ,table = layui.table
  ,form = layui.form
  ,http_proxy = layui.http_proxy;
  //设备台账的资材信息列表
  table.render({
    elem: '#LAY-equipment-account'
    ,id:'planTableAll'
    , url: http_proxy('/account/getDeviceList') //模拟接口
    ,parseData:function(res){
      return{
        "code":0
        ,"count":res.total
        ,"data":res.rows
        ,'msg':''
      }
    }
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'productCode', title: '资材编号'}
      ,{field: 'productName', title: '资材名称'}
      ,{field: 'productDes', title: '资材描述'}
      ,{field: 'devTypeValue', title: '资材类型'}
      ,{field: 'lineName', title: '所属线路'}
      ,{field: 'stationName', title: '所属站点'}
      ,{field: 'installDate', title: '安装日期'}
      ,{title: '操作', width: 150, align:'center', fixed: 'right', toolbar: '#table-useradmin-webuser'}
    ]]
    ,where:{
      fMcId: jTools.getQuery('fMcId',''),
      stationId: jTools.getQuery('stationId',''),
      keyword: jTools.getQuery('keyword','')
    }
    ,page: true
    ,limit: 10
    ,limits: [10, 15, 20, 25, 30]
    ,text: {
      none:'暂无数据'
    }
  });
  
  //监听工具条
  table.on('tool(LAY-user-manage)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.prompt({
        formType: 1
        ,title: '敏感操作，请验证口令'
      }, function(value, index){
        layer.close(index);
        
        layer.confirm('真的删除行么', function(index){
          obj.del();
          layer.close(index);
        });
      });
    } else if(obj.event === 'edit'){
      var tr = $(obj.tr);

      layer.open({
        type: 2
        ,title: '编辑用户'
        ,content: '../../../views/user/user/userform.html'
        ,maxmin: true
        ,area: ['500px', '450px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submitID = 'LAY-user-front-submit'
          ,submit = layero.find('iframe').contents().find('#'+ submitID);

          //监听提交
          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
            var field = data.field; //获取提交的字段
            
            //提交 Ajax 成功后，静态更新表格中的数据
            //$.ajax({});
            table.reload('LAY-user-front-submit'); //数据刷新
            layer.close(index); //关闭弹层
          });  
          
          submit.trigger('click');
        }
        ,success: function(layero, index){
          
        }
      });
    }
  });

  //设备维护计划
  table.render({
    elem: '#LAY-equipment-plan'
    , url: http_proxy('/dev/maintenance/planList') //数据接口
    , page: true //开启分页
    , method: 'post'
    ,id:'planTableAll'
    , response: {
      countName: '' //规定数据总数的字段名称，默认：count
      , dataName: 'data' //规定数据列表的字段名称，默认：data
      , roleName: ''
    }
    ,cols: [[
      {field: 'specialtyValue', title: '专业'}
      ,{field: 'systemValue', title: '系统名'}
      ,{field: 'devTypeValue', title: '设备类型'}
      ,{field: 'productName', title: '设备名称',width: 100}
      ,{field: 'installPosition', title: '安装位置'}
      ,{field: 'stationName', title: '站点',width: 100}
      ,{field: 'maintenanceFlag', title: '维修方式', templet:'#forMaintenanceFlag'}
      ,{field: 'currentMaintenanceDate', title: '本次计划维修时间'}
      ,{field: 'nextMaintenanceDate', title: '下次计划维修时间'}
      ,{field: 'distanceMaintenanceDays', title: '距离下次计划维修天数'}
      ,{field: 'taskStatus', title: '处置状态',width: 80, templet:'#forTaskStatus'}
      ,{title: '操作', width: 150, align:'center', fixed: 'right', toolbar: '#planOperate'}
    ]]
  });
  //监听工具条
  table.on('tool(LAY-equipment-plan)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('真的删除此行么？', function(index){
        $.ajax({
          url:http_proxy('/materialCaregory/deleteMcAttr'),
          type:'post',
          data:{
            fCaId:data.fCaId
          },
          success:function(data){
            if(data.code = "success" ){
              obj.del();
              layer.close(index);
              layer.msg(data.message,{icon:6});
              tableIns.reload()
            }else{
              layer.msg(data.message, {icon: 5});
            }
          }
        });
        return false;
      });
    } else if(obj.event === 'edit'){
      debugger
      layer.open({
        type: 2
        ,title: '查看详情'
        ,content: 'plan-editForm.html?productId='+ data.productId
        ,maxmin: true
        ,area: ['80%', '80%']
        ,success: function(layero, index){
          let iframe = window['layui-layer-iframe'+index];
          iframe.initData(data);
        }
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submit = layero.find('iframe').contents().find('#LAY-equipment-plan-submit');
          //监听提交
          iframeWindow.layui.form.on('submit(LAY-equipment-plan-submit)', function(data){
            debugger
            var field = data.field; //获取提交的字段
            field.productId =  obj.data.productId;

            $.ajax({
              url:http_proxy('/dev/maintenance/saveOrUpdateTask'),
              type:'post',
              data:field,
              success:function(data){
                if(data.code=='0' ){
                  debugger
                  layer.msg(data.message,{icon:6});
                  table.reload('maintenanceRecord')
                }else{
                  layer.msg(data.message, {icon: 5});
                }
              }
            });
            return false;
            // table.reload('LAY-user-front-submit'); //数据刷新
            layer.close(index); //关闭弹层
            debugger
          });  
          
          submit.trigger('click');
        }
      });
    }
  });
  //设备检修记录
  table.render({
    elem: '#LAY-equipment-record'
    , url: http_proxy('/dev/check/repair/list') //模拟接口
    , page: true //开启分页
    , method: 'post'
    , response: {
      countName: '' //规定数据总数的字段名称，默认：count
      , dataName: 'data' //规定数据列表的字段名称，默认：data
      , roleName: ''
    }
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'taskNo', width: 80, title: '工单号'}
      ,{field: 'productName', title: '设备名称'}
      ,{field: 'stationName', title: '站点名称'}
      ,{field: 'devTypeValue', title: '设备类型'}
      ,{field: 'systemValue', title: '系统名称'}
      ,{field: 'specialtyValue', title: '专业'}
      ,{field: 'maintenanceDate', title:'检修日期'}
      ,{field: 'maintenanceContent', title: '检修内容'}
      ,{field: 'maintenancePersonName', title: '检修人'}
      ,{field: 'maintenanceFlag', title: '维修方式'}
      ,{field: 'taskStatus', title: '工单状态'}
      ,{field: 'remark', title: '描述'}
    ]]
    ,text: '对不起，加载出现异常！'
  });
  
  //监听工具条
  table.on('tool(LAY-user-back-manage)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.prompt({
        formType: 1
        ,title: '敏感操作，请验证口令'
      }, function(value, index){
        layer.close(index);
        layer.confirm('确定删除此管理员？', function(index){
          console.log(obj)
          obj.del();
          layer.close(index);
        });
      });
    }else if(obj.event === 'edit'){
      var tr = $(obj.tr);

      layer.open({
        type: 2
        ,title: '编辑管理员'
        ,content: '../../../views/user/administrators/adminform.html'
        ,area: ['420px', '420px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submitID = 'LAY-user-back-submit'
          ,submit = layero.find('iframe').contents().find('#'+ submitID);

          //监听提交
          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
            var field = data.field; //获取提交的字段
            
            //提交 Ajax 成功后，静态更新表格中的数据
            //$.ajax({});
            table.reload('LAY-user-front-submit'); //数据刷新
            layer.close(index); //关闭弹层
          });  
          
          submit.trigger('click');
        }
        ,success: function(layero, index){           
          
        }
      })
    }
  });

  //设备履历
  table.render({
    elem: '#LAY-equipment-resume'
      , url: http_proxy('/dev/list') //模拟接口
    ,cols: [[
      {field: 'index', width: 80, title: '#',templet:'#index'}
      ,{field: 'productCode', title: '资材编号'}
      ,{field: 'productName', title: '资材名称'}
      ,{field: 'productDes', title: '资材描述'}
      ,{field: 'devTypeValue', title: '资材类型'}
      ,{field: 'lineName', title: '所属线路'}
      ,{field: 'stationName', title: '所属站点'}
      ,{field: 'installDate', title: '安装日期'}
      ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
    ]]
    ,text: '对不起，加载出现异常！'
  });
  
  //监听工具条
  table.on('tool(LAY-user-back-role)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此角色？', function(index){
        obj.del();
        layer.close(index);
      });
    }else if(obj.event === 'edit'){
      var tr = $(obj.tr);

      layer.open({
        type: 2
        ,title: '编辑角色'
        ,content: '../../../views/user/administrators/roleform.html'
        ,area: ['500px', '480px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

          //监听提交
          iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
            var field = data.field; //获取提交的字段
            
            //提交 Ajax 成功后，静态更新表格中的数据
            //$.ajax({});
            table.reload('LAY-user-back-role'); //数据刷新
            layer.close(index); //关闭弹层
          });  
          
          submit.trigger('click');
        }
        ,success: function(layero, index){
        
        }
      })
    }
  });
  exports('equipment', {})
});